import com.sun.source.tree.Tree;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86136
 * Date: 2024-11-28
 * Time: 23:02
 */
public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(int val) {
            this.val = val;
        }
    }
    public TreeNode root = null;
    public TreeNode parent = null;

    public void insert(int key) {
        TreeNode node = new TreeNode(key);

        if(root == null) {
            root = node;
        }
        TreeNode cur = root;
        while(cur != null) {
            if(node.val == cur.val) {
                return;
            }
            else if (node.val > cur.val) {
                parent = cur;
                cur = cur.right;
            } else {
                parent = cur;
                cur = cur.left;
            }
        }
        if(node.val < parent.val) {
            parent.left = node;
        }else {
            parent.right = node;
        }
    }
    public TreeNode find(int key) {
        TreeNode cur = root;
        while(cur != null) {
            if(cur.val == key) {
                return cur;
            }else if(cur.val > key) {
                cur = cur.left;
            }else {
                cur = cur.right;
            }
        }
        return null;
    }
    public void remove(int key) {
        TreeNode cur = root;
        while(cur != null) {
            if(cur.val == key) {
                removeNode(parent,cur);
                break;
            }else if(cur.val > key) {
                parent = cur;
                cur = cur.left;
            }else {
                parent = cur;
                cur = cur.right;
            }
        }
    }

    private void removeNode(TreeNode parent, TreeNode cur) {
        if(cur.left == null) {
            if(cur == root) {
                root = cur.right;
            } else if (cur == parent.left) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = root.left;
            }else if(cur == parent.left) {
                parent.left = root.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            /*TreeNode targetParent = cur;
            TreeNode target = cur.left;
            while(target.right != null) {
                targetParent = target;
                target = target.right;
            }
            cur.val = target.val;
            if(targetParent.left == target) {
                targetParent.left = target.left;
            }else {
                cur.right = targetParent.left;
            }*/
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while(target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.val = target.val;
            if(targetParent.right == target)  {
                targetParent.right = target.right;
            }else {
                targetParent.left = target.right;
            }
        }
    }
}
